package com.srolorz.fmtp.thread.pool.executor.pool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author eleven
 */
public class DefaultRefuseThreadPoolHandler implements RejectedExecutionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRefuseThreadPoolHandler.class);

    public static DefaultRefuseThreadPoolHandler build() {
        return BeanNewInstanceFactory.NEW_INSTANCE;
    }

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {

        try {
            LOGGER.info("put current thread into queue! threadName={},threadId={}",
                    Thread.currentThread().getName(),Thread.currentThread().getId());
            executor.getQueue().put(r);
        } catch (InterruptedException e) {
            LOGGER.error("failed to put current thread to queue!", e);
        }
    }


    public static final class BeanNewInstanceFactory {
        private static final DefaultRefuseThreadPoolHandler NEW_INSTANCE = new DefaultRefuseThreadPoolHandler();
    }
}




